package com.cxs.serviceabilitydemo.connections;

import ohos.rpc.*;

// 对端实现的客户端
public class MyRemote extends RemoteObject implements IRemoteBroker {
    static final int ERR_OK = 0;    // 通信成功的code值
    static final int ERROR = -1;    // 通信失败的code值
    static final int COMMAND_PLUS = IRemoteObject.MIN_TRANSACTION_ID;



    public MyRemote() {
        super("MyService_Remote");
    }

    @Override
    public IRemoteObject asObject() {
        return this;
    }

    // 接受发起连接的端侧发送过来的数据，并处理这些数据，然后返回
    @Override
    public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
        if (code != COMMAND_PLUS) {
            reply.writeInt(ERROR);
            return false;
        }
        int value1 = data.readInt();
        int value2 = data.readInt();
        int sum = value1 + value2;
        reply.writeInt(ERR_OK);
        reply.writeInt(sum);
        System.out.println("onRemoteRequest sum=" + sum);
        return true;
    }
}
